package com.classeven.downhill.element;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;

import org.andengine.opengl.texture.ITexture;

import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;

import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


import com.classeven.downhill.engine.Anchor;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;
import com.classeven.downhill.game.element.GameElementAnimation;
import com.classeven.downhill.game.element.GameElementSprite;

public class AAnimatedSprite 
{
	AnimatedSprite spr;
	
	GameEngine theEngine;

//	BitmapTextureAtlas bbta;
//	TextureRegion ttr;
	
	public AAnimatedSprite(GameEngine mainActivity, GameElementAnimation[] Array_spr, int index)
	{
		this.theEngine = mainActivity;
		Load(Array_spr, index);
	}
	
	public void Load(GameElementAnimation[] Array_spr, int index)
	{

		final String path = Array_spr[index].getPath();


//		Bitmap bitmap;

		try {

			Bitmap bitmap = BitmapFactory.decodeStream(theEngine.getAssets().open(Array_spr[index].getPath()));
			BitmapTextureAtlas gameSpriteITexture = new BitmapTextureAtlas(theEngine.getTextureManager(), bitmap.getWidth(), bitmap.getHeight());
					
//					(theEngine.getTextureManager(), new IInputStreamOpener() 
//				{
//					public InputStream open() throws IOException 
//					{
//						return theEngine.getAssets().open(path);
//					}
//				});
			gameSpriteITexture.load();
			TiledTextureRegion gameSpriteTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
					gameSpriteITexture, theEngine, Array_spr[index].getPath(),0 ,0,  Array_spr[index].getCol(), Array_spr[index].getRow());
							
			spr = new AnimatedSprite(Utils.GetRatio(Array_spr[index].getX()),
					Utils.GetRatio(Array_spr[index].getY()),
					Utils.GetRatio(Array_spr[index].getW()),
					Utils.GetRatio(Array_spr[index].getH()),
					gameSpriteTextureRegion.deepCopy(),
					theEngine.getVertexBufferObjectManager());

			float x, y;
			x = Utils.GetAnchorX(spr.getWidth(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getX());
			y = Utils.GetAnchorY(spr.getHeight(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getY());
			
			spr.setPosition(x, y);
//			spr.setFlippedHorizontal(Array_spr[index].isFlippedHorizontal());
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		

		float x, y;
		x = Utils.GetAnchorX(spr.getWidth(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getX());
		y = Utils.GetAnchorY(spr.getHeight(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getY());
		
		spr.setPosition(x, y);
//		spr.setFlippedHorizontal(Array_spr[index].isFlippedHorizontal());

	}
	
	public void AddText(AText text)
	{
		spr.attachChild(text.theText());
		int x, y;
		x = (int) (spr.getWidth()/2 - text.theText().getWidth()/2);
		y = (int) (spr.getHeight()/2 - text.theText().getHeight()/2);
		text.theText().setPosition(x, y);
	}
	
	public void AddLeftText(AText text)
	{
		spr.attachChild(text.theText());
		int x, y;
		x = (int) (spr.getWidth());
		y = (int) (spr.getHeight()/2 - text.theText().getHeight()/2);
		text.theText().setPosition(x, y);
	}
	
	public void AddSprite(AAnimatedSprite sprite)
	{
		spr.attachChild(sprite.theSprite());
		int x, y;
		x = (int) (spr.getWidth()/2 - sprite.theSprite().getWidth()/2);
		y = (int) (spr.getHeight()/2 - sprite.theSprite().getHeight()/2);
		sprite.theSprite().setPosition(x, y);
	}
	
	public AnimatedSprite theSprite()
	{
		return spr;
	}
	
	public void SetPos(float x, float y, Anchor anchor)
	{
		float _x, _y;
		_x = Utils.GetAnchorX(spr.getWidth(), anchor) + Utils.GetRatio(x);
		_y = Utils.GetAnchorY(spr.getHeight(), anchor) + Utils.GetRatio(y);
		
		SetPos(_x, _y);
	}
	
	public void SetPos(float x, float y)
	{
		spr.setPosition(x, y);
	}
	
	public void setScale(float scaleRatio)
	{
		spr.setScale(scaleRatio);
	}
	
	public void DetachSelf()
	{
		spr.detachSelf();
	}
	
	public float GetX()
	{
		return spr.getX();
	}
	
	public float GetY()
	{
		return spr.getY();
	}
	
	public void unLoad()
	{
//		bbta.unload();
	}
	
	
	
}
